<?php
/**
 * Created by PhpStorm .
 * User: 杜磊磊
 * Date: 2020/7/12
 * Time: 15:32
 */
namespace app\common\model\mysql;

use think\Model;

class Category extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 查询表中字段为 id pid name 的所有数据
     * @param string $field
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalCategory($field = "*")
    {
        $where = [
            "status" => config("status.table_normal"),
        ];
        $result = $this->where($where)->field($field)->select();
        return $result;
    }


    public function getSortByName($name)
    {
        $where = [
          'name' => $name
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    public function getLists($where,$num = 10)
    {
        $order = [
          'listorder' => 'desc',
          'id' => 'desc'
        ];
        $result = $this->where("status","<>",99)
            ->where($where)
            ->order($order)
            ->paginate($num);
//        echo $this->getLastSql();exit;
        return $result;
    }

    /**
     * 根据id更新数据
     * @param $id
     * @param $data
     * @return bool
     * Author: 杜磊磊
     * Date: 2020/9/24 9:37
     */
    public function updateById($id,$data)
    {
        $data['update_time'] = time();
        return $this->where(['id'=>$id])->save($data);
    }
}